package com.zrrd.blog.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.blog.article.req.CategoryREQ;
import com.zrrd.blog.article.service.ICategoryService;
import com.zrrd.blog.api.entities.Category;
import com.zrrd.blog.article.mapper.CategoryMapper;
import com.zrrd.blog.util.base.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Override
    public Result queryPage(CategoryREQ req) {
        //select * from category
        QueryWrapper<Category> w=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(req.getName())){
            //where name like req.getName()
            w.like("name",req.getName());
        }
        if(req.getStatus()!=null){
            //and status = 1
            w.eq("status",req.getStatus());
        }
        //order by status desc,sort asc
        w.orderByDesc("status").orderByAsc("sort");
        //limit current * size,size
        IPage<Category> page=baseMapper.selectPage(req.getPage(),w);

        return Result.ok(page);
    }

    @Override
    public boolean updateById(Category entity) {
        entity.setUpdateDate(new Date());
        return super.updateById(entity);
    }

    @Override
    public Result findAllNormal(int status) {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("status",status);
        return Result.ok(baseMapper.selectList(wrapper));
    }

    @Override
    public Result findCategoryAndLabel() {
        return Result.ok(baseMapper.findCategoryAndLabel());
    }
}
